#!/bin/bash
cd `dirname $0`

gradle_path=/usr/lib/gradle/gradle-3.4/bin/gradle
#gradle_path=gradle

usage() {
    echo "Usage: $0 [-c <massage_chair|massage_chair_dev|massage_chair_test>] [-m <massage_chair|massage_chair_dev|massage_chair_test>]" 1>&2;
    exit 1;
}

while getopts ":c:m:" o; do
    case "${o}" in
        c)
            c=${OPTARG}
            ((c == massage_chair || c == massage_chair_dev || c == massage_chair_test)) || usage
            ;;
        m)
            m=${OPTARG}
            ((m == massage_chair || m == massage_chair_dev || m == massage_chair_test)) || usage
            ;;
        *)
            usage
            ;;
    esac
done
shift $((OPTIND-1))

if [ -n "${c+x}" ]; then
    ${gradle_path} database:flywayClean -Dflyway.schemas=${c}
fi

if [ -n "${m+x}" ]; then
    ${gradle_path} database:flywayMigrate -Dflyway.schemas=${m}
fi

${gradle_path} app:build -x test